package com.shop.cereshop.app.dl.logic.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class WeixinUtil {


    /**
     * 功能描述:
     * @Param: [appId, appSecret]
     * @return: java.util.Map
     * @Author: 况鑫威
     * @Date: 2021/7/12
     * @Time: 10:53
     * 需要配置公网ip白名单才能正常使用
     */
    public static String getAccessToken(String appId, String appSecret) {
        /**
         * wxCode:用户登录时获取的code
         * appid：小程序的Appid
         * appid：小程序的秘钥
         */
        String host = "http://service.zhuxiaoke.cn/Tongcheng/GetAccessToken";
        String path = "";
        //把数据放入请求体中
        HashMap<String, String> body = new HashMap<>();
        body.put("appid", appId);
        body.put("appSecret", appSecret);
//        body.put("grant_type", "client_credential");
        String data = "";
        try {
            //获取获取access_token
            HttpResponse response = HttpUtils.doGet(host, path, "GET", new HashMap<>(), body);
            //json数据
            data = EntityUtils.toString(response.getEntity());
            System.out.println(data);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return data;
    }



    public static String getAccessTokenTwo(String appId, String appSecret) {
        /**
         * wxCode:用户登录时获取的code
         * appid：小程序的Appid
         * appid：小程序的秘钥
         */
        String host = "https://api.weixin.qq.com";
        String path = "/cgi-bin/token";
        //把数据放入请求体中
        HashMap<String, String> body = new HashMap<>();
        body.put("appid", appId);
        body.put("secret", appSecret);
        body.put("grant_type", "client_credential");
        String data = "";
        try {
            //获取获取access_token
            HttpResponse response = HttpUtils.doGet(host, path, "GET", new HashMap<>(), body);
            //json数据
            data = EntityUtils.toString(response.getEntity());
            System.out.println(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (String)JSONObject.parseObject(data, Map.class).get("access_token");
    }


//	public static Map<String, Object> getUserInfo(String accessToken, String openId){
//		Map<String, Object> paramsMap = new HashMap<String, Object>();
//		paramsMap.put("access_token", accessToken);
//		paramsMap.put("openid", "otYOUweAts_XQjUyBmswG-WK5hLU");
//		paramsMap.put("lang", "zh_CN");
////		String userInfo = HttpUtil.httpsDoPost("https://api.weixin.qq.com/cgi-bin/user/info", paramsMap, "utf-8");
//		String userInfo = HttpUtil.httpsDoGet(
//				"https://api.weixin.qq.com/cgi-bin/user/info?"
//						+ "access_token=" + accessToken + "&"
//						+ "openid=otYOUwVKJ8ykGewank4cSvae2Y1Q&"
//						+ "lang=zh_CN",
//				"utf-8");
//		System.out.println(userInfo);
//	}

    public static String getNonceStr() {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String res = "";
        Random rd = new Random();
        for (int i = 0; i < 16; i++) {
            res += chars.charAt(rd.nextInt(chars.length() - 1));
        }
        return res;
    }

    public static HashMap<String, Object> getUserInfo(String accessToken, String openid){
        /**
         * access_token:token
         * openid：小程序的Appid
         */
        String host = "https://api.weixin.qq.com";
        String path = "/sns/userinfo";
        //把数据放入请求体中
        HashMap<String, String> body = new HashMap<>();
        body.put("access_token", accessToken);
        body.put("openid", openid);
        body.put("lang", "zh_CN");
        String data = "";
        try {
            //获取获取access_token
            HttpResponse response = HttpUtils.doGet(host, path, "GET", new HashMap<>(), body);
            //json数据
            data = EntityUtils.toString(response.getEntity());
            System.out.println(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(data, HashMap.class);
    }

    public static JSONObject refreshToken(String refreshToken, String appid){
        /**
         * wxCode:用户登录时获取的code
         * appid：小程序的Appid
         * appid：小程序的秘钥
         */
        String host = "https://api.weixin.qq.com";
        String path = "/sns/oauth2/refresh_token";
        //把数据放入请求体中
        HashMap<String, String> body = new HashMap<>();
        body.put("refresh_token", refreshToken);
        body.put("appid", appid);
        body.put("grant_type", "refresh_token");
        String data = "";
        try {
            //获取获取access_token
            HttpResponse response = HttpUtils.doGet(host, path, "GET", new HashMap<>(), body);
            //json数据
            data = EntityUtils.toString(response.getEntity());
            System.out.println(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(data, JSONObject.class);
    }



    public static boolean isTokenFlag(Map resultMap) {
        return resultMap.get("errcode") == null;
    }
}














